<template>

  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <div class="filter-item">
        <el-input v-model="workerTypeQuery.param.name" placeholder="请输入用户名" style="width: 200px;" clearable />
        <el-button type="primary" icon="el-icon-search" @click="handleFilter" style="margin-left:10px;">
          查询
        </el-button>
        <el-button v-if="add_worker_type_button_status" icon="el-icon-plus" @click="handleAdd" style="margin-left:10px;">
          增加
        </el-button>
      </div>
    </div>

    <el-table :data="workerTypeLists" style="width: 100%" v-loading.body="listLoading" border="" fit="" highlight-current-row="">
      <el-table-column v-if="false" prop="id" />
      <el-table-column label="名称" prop="name" />
      <el-table-column label="备注" prop="desc" />
      <el-table-column label="参考价格" prop="price" />
      <el-table-column fixed="right" label="操作" width="300" v-if="upd_worker_type_button_status || del_worker_type_button_status">
        <template slot-scope="scope">
          <el-button v-if="upd_worker_type_button_status" type="primary" icon="el-icon-edit" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button v-if="del_worker_type_button_status" type="danger" icon="el-icon-del" size="mini" @click="handleDel(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="workerTypeQuery.page" :page-size="workerTypeQuery.size" :page-sizes="[25, 50, 100]" layout="total, sizes, prev, pager, next, jumper" :total="totalWorkerType">
    </el-pagination>

    <el-dialog :title="textMap[formDialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false" width="45%">
      <el-form ref="form" :model="workerTypeForm" label-width="80px">
        <el-form-item label="名称" prop="name" :rules="[{ required: true, message: '请填写工种名称', trigger: 'blur' },{ min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }]">
          <el-input v-model="workerTypeForm.name"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="desc" :rules="[{ min: 1, max: 250, message: '长度在 1 到 50 个字符', trigger: 'blur' }]">
          <el-input v-model="workerTypeForm.desc"></el-input>
        </el-form-item>
        <el-form-item label="参考价格" prop="price">
          <el-input v-model.number="workerTypeForm.price" type="number"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer" style="text-align:center;">
        <el-button @click="resetForm">
          取 消
        </el-button>
        <el-button v-if="formDialogStatus==='create'" type="primary" @click="handleCreate()">
          确 定
        </el-button>
        <el-button v-if="formDialogStatus==='update'" type="primary" @click="handleUpdate()">
          确 定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters } from 'vuex';
import { addWorkerTypeInfo, updWorkerTypeInfo, delWorkerTypeInfo, queryworkerTypeInfo, queryAllWorkerTypeInfo } from '@/api/workerTypeInfo.js';
export default {
  name: 'workerTypeInfo',
  data() {
    return {
      textMap: {
        create: '创建',
        update: '修改'
      },
      workerTypeQuery: {
        page: 1,
        size: 25,
        param: {
          name: undefined
        }
      },
      workerTypeLists: [],
      totalWorkerType: 0,
      formDialogStatus: undefined,
      dialogFormVisible: false,
      workerTypeForm: {
        id: undefined,
        name: undefined,
        desc: undefined,
        price: 0
      },
      listLoading: false,
      successCode: 20000,

      add_worker_type_button_status: false,
      upd_worker_type_button_status: false,
      del_worker_type_button_status: false
    }
  },
  computed: {
    ...mapGetters(["elements"])
  },
  created() {
    this.handleFilter();
    if (this.elements['add_worker_type']) {
      this.add_worker_type_button_status = true;
    }
    if (this.elements['upd_worker_type']) {
      this.upd_worker_type_button_status = true;
    }
    if (this.elements['del_worker_type']) {
      this.del_worker_type_button_status = true;
    }
  },
  methods: {
    getWorkerTypeLists() {
      this.listLoading = true;
      queryworkerTypeInfo(this.workerTypeQuery).then(resp => {
        var data = resp.datas;
        this.workerTypeLists = data.datas;
        this.totalWorkerType = data.total;
        this.listLoading = false;
      })
        .catch(() => {
          this.listLoading = false;
        });
    },
    handleFilter() {
      this.workerTypeQuery.page = 1;
      this.getWorkerTypeLists();
    },
    handleAdd() {
      this.resetForm();
      this.dialogFormVisible = true;
      this.formDialogStatus = 'create';
    },
    handleCreate() {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return false;
        }
        addWorkerTypeInfo(this.workerTypeForm).then(resp => {
          this.resetForm();
          this.getWorkerTypeLists();
          this.$notify({
            title: "成功",
            message: "创建成功",
            type: "success",
            duration: 2000
          });
        });
      });
    },
    handleEdit(row) {
      this.resetForm();
      this.dialogFormVisible = true;
      this.formDialogStatus = 'update';
      this.$nextTick(() => {
        this.workerTypeForm = {
          id: row.id,
          name: row.name,
          desc: row.desc,
          price: row.price
        };
      });
    },
    handleUpdate() {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return false;
        }
        updWorkerTypeInfo(this.workerTypeForm).then(resp => {
          this.resetForm();
          this.getWorkerTypeLists();
          this.$notify({
            title: "成功",
            message: "修改成功",
            type: "success",
            duration: 2000
          });
        });
      })
    },
    handleDel(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        delWorkerTypeInfo(row.id).then(() => {
          this.$notify({
            title: "成功",
            message: "删除成功",
            type: "success",
            duration: 2000
          });
          this.getWorkerTypeLists();
        })
          .catch(() => {
            this.getWorkerTypeLists();
          });
      });
    },
    handleCurrentChange(val) {
      this.workerTypeForm.page = val;
      this.getWorkerTypeLists();
    },
    handleSizeChange(val) {
      this.workerTypeForm.size = val;
      this.getWorkerTypeLists();
    },
    resetForm() {
      this.dialogFormVisible = false;
      this.formDialogStatus = undefined;
      if (this.$refs['form']) {
        this.$refs['form'].resetFields();
      }
    }
  }
}
</script>